import { del, get, post, put } from "@/utils/request";

// 定义响应数据类型
export interface UserInfo {
    id: string;
    username: string;
    password: string;
    gender: "male" | "female" | "other";
    email: string;
    avatar: string;
}

// GET请求
export const getUserAPI = (userId: string) => {
    return get<UserInfo>(`/user/${userId}`, { id: userId });
};

export const getAllUsersAPI = () => {
    return get<UserInfo[]>(`/users`);
};

// POST请求
interface LoginParams {
    username: string;
    password: string;
}

interface LoginResponse {
    token: string;
    expiresIn: number;
}

export const loginAPI = (username: string, password: string) => {
    const params: LoginParams = { username, password };
    return post<any>("/login", params, {
        loadingText: "登录中...",
        isAuth: false // 登录接口不需要认证
    });
};

export const registerAPI = (username: string, password: string) => {
    return post("/users", {
        username,
        password,
        gender: "other",
        email: "",
        avatar: `https://api.dicebear.com/9.x/bottts-neutral/svg?seed=${Date.now()}`
    });
};

export const updateUserinfoAPI = (data: Partial<UserInfo>) => {
    return put<UserInfo>(`/users/${data.id}`, data);
};

export const getHomeDataAPI = () => {
    return get("/home");
};

export const getMoviesAPI = () => {
    return get("/movies");
};
export const getMovieDetailAPI = (id?: string) => {
    return get(`/movieDetail`);
};

export const getCollectsAPI = (userId: string) => {
    return get<any[]>(`/collects?userId=${userId}`);
};

export const getCollectAPI = (userId: string, movieId: string) => {
    return get<any[]>("/collects", { userId, movieId });
};

export const addCollectAPI = (userId: string, movieId: string, data: any) => {
    return post("/collects", { userId, movieId, ...data });
};

export const deleteCollectAPI = (id: string) => {
    return del(`/collects/${id}`);
};
